package org.netspeak.android.utils;

import org.netspeak.application.generated.NetspeakMessages.Phrase;
import org.netspeak.client.CommonUtils;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

/**
 *
 * @author Teresa Holfeld, teresa.holfeld@gmail.com
 */
public class ParcelablePhrase implements Parcelable {
    
    /** Tag for LogCat logging */
    private static final String TAG = "ParcelablePhrase";
    
    private String mPhraseText;
    private long mPhraseFrequency;
    
    public ParcelablePhrase(Phrase phrase) {
        mPhraseText = CommonUtils.toString(phrase);
        mPhraseFrequency = phrase.getFrequency();
    }
    
    /**
     * Constructor used by {@link ParcelablePhraseCreator}.
     * 
     * @param source    parcel to be un-marshalled to ParcelablePhrase
     */
    public ParcelablePhrase(Parcel source){
          Log.v(TAG, "Un-marshalling Parcel to create ParcelablePhrase");
          mPhraseText = source.readString();
          mPhraseFrequency = source.readLong();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        Log.v(TAG, "writeToParcel... " + flags);
        dest.writeString(mPhraseText);
        dest.writeLong(mPhraseFrequency);
    }

    /**
     * {@link Creator} implementation, required during un-marshaling the data
     * stored in a Parcel.
     */
    public static final Parcelable.Creator<ParcelablePhrase> CREATOR = 
            new Parcelable.Creator<ParcelablePhrase>() {

        public ParcelablePhrase createFromParcel(Parcel source) {
            return new ParcelablePhrase(source);
        }

        public ParcelablePhrase[] newArray(int size) {
            return new ParcelablePhrase[size];
        }
    };
    
    public String getText() {
        return mPhraseText;
    }
    
    public long getFrequency() {
        return mPhraseFrequency;
    }
}
